<template>
  <div class="login">
    <vue-particles
        color="#64f4e5"
        :particleOpacity="0.3"
        :particlesNumber="100"
        shapeType="circle"
        :particleSize="30"
        linesColor="#64f4e5"
        :linesWidth="1"
        :lineLinked="false"
        :lineOpacity="0.4"
        :linesDistance="150"
        :moveSpeed="10"
        :hoverEffect="true"
        hoverMode="bubble"
        :clickEffect="true"
        clickMode="push"
        class="lizi"
      ></vue-particles>
    <el-form ref="form" :model="form" :rules="rules" class="form">
      <el-form-item>
        <img src="../assets/avatar.jpg" class="login-img">
      </el-form-item>
      <el-form-item prop="userName">
        <el-input v-model="form.userName" 
                placeholder="用户名" 
                prefix-icon="el-icon-phone-outline">
        </el-input>
      </el-form-item>
      <el-form-item  prop="password">
        <el-input v-model="form.password"
                placeholder="密码"
                type="password"
                @keydown.enter.native="login('form')"
                prefix-icon="el-icon-view">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" @click="login('form')">登录</el-button>
      </el-form-item>
    </el-form>
    
  </div>
</template>
<script>
import { loginPost } from "@/api";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      form: {
        userName: "",
        password: ""
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          loginPost(this.form).then(res => {
            if (res.data.status) {
              // 表示登入成功 就应该跳转到欢迎页
              // 保存到localstorage
              console.log(res.data);
              try {
                const info = {
                  userName: res.data.info.userName,
                  nickName: res.data.info.nickName
                };
                //先通过mutation改变vuex里面的值
                this.saveName(res.data.info.userName);
                this.saveNickName(res.data.info.nickName);
                //存到localhost里面 防止刷新名字小时
                localStorage.setItem("userName", JSON.stringify(info));
                this.$router.push("/");
              } catch (e) {
                alert("不要重复登入");
              }
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    ...mapMutations(["saveName", "saveNickName"])
  }
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: #2f4050;
  position: fixed;
  .form {
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: #fff;
    .login-img {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .btn {
      width: 100%;
    }
  }
}
.lizi {
  position: absolute;
  width: 100%;
  height: 100%;
}
</style>


